package graphics.screens.mode;

import graphics.Background;
import graphics.BootColor;
import graphics.Theme;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import state.State;
import tool.KeyCode;
import tool.ScreenUtils;
import boot.Boot;

public class OptionScreen1 extends OptionScreen implements CommandListener {

	private static final int ANCHOR = Graphics.LEFT | Graphics.TOP;

	private int index;
	private PlayModeSaver modeSaver;

	public OptionScreen1() {
		modeSaver = new PlayModeSaver();
		addCommand(cancelCommand);
		setCommandListener(this);
	}

	protected void paint(Graphics g) {
		ScreenUtils.setBlackBackground(g);

		Background bg = new Background();
		bg.repaint(g);

		drawMenuEntries(g);
	}

	protected void drawMenuEntries(Graphics g) {

		index = 0;
		int height = g.getFont().getHeight();
		g.setColor(graphics.BootColor.WHITE);

		String text = (index + 1) + " - Mode";
		g.drawString(text, X_MENU_OFFSET, Y_MENU_OFFSET + height * index,
				ANCHOR);

		g.setColor(Theme.getMenuColor());
		if (Boot.state().equals(State.NO_REPEAT_NO_PLAYLIST)) {
			g.drawString("No repeat", X_MENU_OFFSET
					+ g.getFont().stringWidth(text + " "), Y_MENU_OFFSET
					+ height * index, ANCHOR);
		} else if (Boot.state().equals(State.REPEAT_ONE_TUNE)) {
			g.drawString("Loop on 1", X_MENU_OFFSET
					+ g.getFont().stringWidth(text + " "), Y_MENU_OFFSET
					+ height * index, ANCHOR);
		} else if (Boot.state().equals(State.PLAYLIST_MODE)) {
			g.drawString("Playlist", X_MENU_OFFSET
					+ g.getFont().stringWidth(text + " "), Y_MENU_OFFSET
					+ height * index, ANCHOR);
		}
		index++;
		g.setColor(BootColor.WHITE);

		g.drawString((index + 1) + " - Genres list", X_MENU_OFFSET,
				Y_MENU_OFFSET + height * index++, ANCHOR);

		g.drawString((index + 1) + " - Pawn list", X_MENU_OFFSET, Y_MENU_OFFSET
				+ height * index++, ANCHOR);

		g.drawString((index + 1) + " - Reload", X_MENU_OFFSET, Y_MENU_OFFSET
				+ height * index++, ANCHOR);

	}

	public void commandAction(Command command, Displayable dis) {
		if (command == cancelCommand) {
			Boot.getInstance().readyToGo();
		}
	}

	public void keyPressed(int keyCode) {

		if (keyCode == KeyCode.ONE) {
			modeSaver.switchState();
		} else if (keyCode == KeyCode.TWO) {
			Boot.getInstance().showGenresSuggestions();
		} else if (keyCode == KeyCode.THREE) {
			Boot.showPawnList();
		} else if (keyCode == KeyCode.FOUR) {
			Boot.reload();
			modeSaver.reload();
			Boot.getInstance().readyToGo();
		} else if (keyCode == KeyCode.ASTERISK) {
			Boot.setDisplay(new OptionScreen2());
		}

		repaint();
	}
}
